package com.lcxw.hxzbapi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcxw.hxzbapi.dao.mapper.UnionMemberApplyMapper;
import com.lcxw.hxzbapi.dao.mapper.UnionMemberMapper;
import com.lcxw.hxzbapi.entity.pojo.UnionMemberApply;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class UnionMemberApplyService {

    @Resource
    UnionMemberApplyMapper unionMemberApplyMapper;

    public int insert(UnionMemberApply unionMemberApply) {
        return unionMemberApplyMapper.insert(unionMemberApply);
    }

    public int update(UnionMemberApply unionMemberApply) {
        return unionMemberApplyMapper.updateById(unionMemberApply);
    }

    //通过id查询
    public UnionMemberApply selectById(int id) {
        return unionMemberApplyMapper.selectById(id);
    }


    //工会申请记录查询
    public IPage<UnionMemberApply> getJoinListByUnionId(int unionId, Long userId, String apply,
        int pageNum,
        int pageSize) {

        IPage<UnionMemberApply> page = new Page<>(pageNum, pageSize);

        QueryWrapper<UnionMemberApply> wrapper = new QueryWrapper<>();
        wrapper.eq("unionId", unionId);
        wrapper.eq("apply", apply);
        wrapper.eq(userId != null, "userId", userId);
        wrapper.eq("status", "waiting");
        return unionMemberApplyMapper.selectPage(page, wrapper);
    }

    //转出工会记录查询
    public IPage<UnionMemberApply> getTurnOutByUnionId(int unionId, int pageNum, int pageSize) {
        IPage<UnionMemberApply> page = new Page<>(pageNum, pageSize);

        QueryWrapper<UnionMemberApply> wrapper = new QueryWrapper<>();
        wrapper.eq("turnInUnionId", unionId);
        wrapper.eq("apply", "申请转会");
        wrapper.eq("turnOutStatus", "waiting");
        return unionMemberApplyMapper.selectPage(page, wrapper);
    }

    //转出工会记录查询
    public IPage<UnionMemberApply> getTurnInByUnionId(int unionId, int pageNum, int pageSize) {
        IPage<UnionMemberApply> page = new Page<>(pageNum, pageSize);

        QueryWrapper<UnionMemberApply> wrapper = new QueryWrapper<>();
        wrapper.eq("turnInUnionId", unionId);
        wrapper.eq("apply", "申请转会");
        wrapper.eq("turnOutStatus", "agree");
        wrapper.eq("turnInStatus", "waiting");
        return unionMemberApplyMapper.selectPage(page, wrapper);
    }

    //查询个人申请记录
    public IPage<UnionMemberApply> getMemberRecords(long userId, int pageNum, int pageSize) {
        IPage<UnionMemberApply> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UnionMemberApply> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        return unionMemberApplyMapper.selectPage(page, wrapper);
    }

    //查询用户是否有正在申请加入的记录
    public boolean isJoinApply(long userId) {
        QueryWrapper<UnionMemberApply> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        wrapper.eq("apply", "申请加入");
        wrapper.eq("status", "waiting");
        UnionMemberApply unionMemberApply = unionMemberApplyMapper.selectOne(wrapper);

        return null != unionMemberApply;
    }

    //查询是否正在申请加入或者退出该工会
    public boolean isJoinOrQuitApply(long userId, int unionId, String apply) {
        QueryWrapper<UnionMemberApply> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        wrapper.eq("unionId", unionId);
        wrapper.eq("apply", apply);
        wrapper.eq("status", "waiting");
        UnionMemberApply unionMemberApply = unionMemberApplyMapper.selectOne(wrapper);

        return null != unionMemberApply;
    }
}
